﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ContadorDeHoras.BLL;
using ContadorDeHoras.DTO;

namespace ContadorDeHoras
{
    public partial class FormRelatorio : Form
    {

        private DateTime diainicio;
        private DateTime diafim;

        public FormRelatorio()
        {
            InitializeComponent();
            this.Text = "Geração de relatórios";
            this.dateTimePicker1.Text = (new DateTime(2011,12,19)).ToString();
            this.dateTimePicker2.Text = DateTime.Today.ToString();
        }

        public void parse()
        {
            this.diainicio = Convert.ToDateTime(this.dateTimePicker1.Text);
            this.diafim = (this.checkBox1.Checked ? Convert.ToDateTime(this.dateTimePicker2.Text) : Convert.ToDateTime(null));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            parse();
            if (diainicio != null && diafim != null)
            {
                List<DiaDTO> lista = (diafim == new DateTime()) ? DiaBLL.recuperaMes(new DiaDTO(diainicio, diainicio, diainicio, diainicio, diainicio)) : DiaBLL.recuperaRange(this.diainicio, this.diafim);
                TimeSpan horasnormais = new TimeSpan();
                TimeSpan horasextra = new TimeSpan();
                foreach (DiaDTO d in lista)
                {
                    horasnormais = horasnormais.Add(d.calculaTempoTotal());
                    horasextra = horasextra.Add(d.calculaTempoExtra());
                }
                this.textBox1.Text = string.Format("{0:000} dia(s) {1:00} h:{2:00} min", horasnormais.Days, horasnormais.Hours, horasnormais.Minutes);
                this.textBox2.Text = string.Format("{0:000} dia(s) {1:00} h:{2:00} min", horasextra.Days, horasextra.Hours, horasextra.Minutes);
            }
        }

    }
}
